//
//  fGHomeTableViewController.m
//  fGeekApp
//
//  Created by Administrator on 15/3/6.
//  Copyright (c) 2015年 cwx. All rights reserved.
//+-------------------------------------------------
// 首页模块：主要完成： 1.scrollView ＋ tableView 信息展示
//                  2.城市定位  3.搜索商品功能  4.商品购买
//+-------------------------------------------------
// 涉及的接口：“全部商品信息”展示接口 kHTTP/goods/index
//       http://localhost:8888/fGeekApp/index.php/home/
//+-------------------------------------------------

#import "fGHomeTableViewController.h"
#import "Goods.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import "fGGoodsTableViewCell.h"
#import "Goods.h"
#import "fGGoodsHeaderView.h"
@interface fGHomeTableViewController ()<UITableViewDelegate>

@property(strong,nonatomic)NSMutableArray *goodsArray;//存放每个商品模型的数组

@end

@implementation fGHomeTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.delegate = self;//成为代理
    self.tableView.rowHeight = 100;//设置cell的高度
    
    self.goodsArray = [NSMutableArray array];
    self.title = @"首页";
    
    //请求商品列表
    [self requestGoodsList];
    
}
#pragma mark - 隐藏状态栏
-(BOOL)prefersStatusBarHidden{
    return YES;
}

#pragma mark - AFNetworking框架请求商品列表，异步请求－GET方式
-(void)requestGoodsList{
    
    //1.管理器
    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc]init];
    
    //2.设定类型. (这里要设置request-response的类型)
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    //3.获取url路径
    NSString *urlPath =[NSString stringWithFormat:@"%@goods/allgoods",kHTTP];
    
    //4.请求 －GET方式
    [manager GET:urlPath parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
//                NSLog(@"%@",responseObject);
        //4.1 将请求得到的数据装到字典中
        NSDictionary *responseDict = (NSDictionary *)responseObject;
        
        
        NSString *codeStr = [responseDict valueForKey:@"code"];
        //4.2 判断是否成功连接，code＝1
        if ([codeStr isEqualToString:@"1"]) {
            //4.3 在根据key=data,得到返回字典里面的数组
            NSArray *dataArray = [responseDict valueForKey:@"data"];
            
            //4.4字典转模型
            for (NSDictionary *dict in dataArray) {
                Goods *goods = [Goods getGoodsInstanceByDict:dict];
                
                [self.goodsArray addObject:goods];
            }
            
//            NSLog(@"%@",self.goodsArray);
            //5.刷新得到的数据
            [self.tableView reloadData];
            
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"请求失败，请检查网络!%@",error);
    }];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return [self.goodsArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    fGGoodsTableViewCell *cell = [fGGoodsTableViewCell cellWithTableView:tableView];
    Goods *g = [self.goodsArray objectAtIndex:indexPath.row];
    cell.goods = g;
    
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    fGGoodsHeaderView *tableHead = [fGGoodsHeaderView goodsHeaderView];
    return tableHead;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 200;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
